package cn.dengta.webapp.user.entity.game;

import java.math.BigDecimal;

import cn.dengta.webapp.base.entity.TimeBasedEntity;
import cn.dengta.webapp.user.model.GameDiceBetType;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import lombok.experimental.Accessors;
import me.codeplayer.util.Cmp;

/** 用户下注日志 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("dt_game_dice_bet_log")
@NoArgsConstructor
public class GameDiceBetLog extends TimeBasedEntity {

	/** 用户id */
	private Long userId;
	/** 骰子记录id */
	private Long diceLogId;
	/** 1=骰子；2=转盘；默认为1 */
	private Integer type;
	/** 下注的金币数 */
	private Integer golds;
	/** 倍率 */
	private BigDecimal ratio;
	/** 得到钻石数 */
	private Integer received;
	/** 押注类型： 1=大；2=小；3=单；4=双；5=豹子 */
	private GameDiceBetType betType;
	/** 倍率36 */
	public static final BigDecimal TWENTY_SIX_RATIO = BigDecimal.valueOf(36);
	/** 倍率2 */
	public static final BigDecimal TWO_RATIO = BigDecimal.valueOf(2);
	/** 倍率3 */
	public static final BigDecimal THREE_RATIO = BigDecimal.valueOf(3);

	public GameDiceBetLog(Long userId, Long diceLogId, Integer golds, GameDiceBetType betType) {
		this.userId = userId;
		this.diceLogId = diceLogId;
		this.golds = golds;
		this.betType = betType;
	}

	/** 开奖 */
	public void lottery(GameDiceLog diceLog) {
		/** 骰子开奖 */
		if (Cmp.eq(diceLog.getType(), GameDiceLog.dict_type)) {
			dictLottery(diceLog);
		}
		/** 转盘开奖 */
		if (Cmp.eq(diceLog.getType(), GameDiceLog.roulette_type)) {
			rouletteLottery(diceLog);
		}
	}

	/** 转盘开奖 */
	private void rouletteLottery(GameDiceLog diceLog) {
		BigDecimal ratio;
		switch (this.betType) {
			case ZERO:
				ratio = diceLog.hasZero ? TWENTY_SIX_RATIO : BigDecimal.ZERO;
				break;
			case SINGLE:
				ratio = diceLog.hasSingle ? TWO_RATIO : BigDecimal.ZERO;
				break;
			case DOUBLE:
				ratio = diceLog.hasDouble ? TWO_RATIO : BigDecimal.ZERO;
				break;
			case RED:
				ratio = diceLog.hasRed ? TWO_RATIO : BigDecimal.ZERO;
				break;
			case BLACK:
				ratio = diceLog.hasBlack ? TWO_RATIO : BigDecimal.ZERO;
				break;
			case TWELVE:
				ratio = diceLog.hasTwelve ? THREE_RATIO : BigDecimal.ZERO;
				break;
			case TWENTY_FOUR:
				ratio = diceLog.hasTwentyFour ? THREE_RATIO : BigDecimal.ZERO;
				break;
			case THIRTY_SIX:
				ratio = diceLog.hasThirtySix ? THREE_RATIO : BigDecimal.ZERO;
				break;
			default:
				ratio = BigDecimal.ZERO;
				break;
		}
		this.setRatio(ratio);
		this.setReceived(BigDecimal.ZERO.equals(ratio) ? ratio.intValue() : BigDecimal.valueOf(this.golds).multiply(ratio).intValue());
	}

	/** 骰子开奖 */
	private void dictLottery(GameDiceLog diceLog) {
		if (diceLog.hasLeopard()) {
			if (this.betType == GameDiceBetType.LEOPARD) {
				this.setRatio(GameDiceBetType.LEOPARD.getRatio());
				this.setReceived(GameDiceBetType.LEOPARD.getRatio().intValue() * golds);
			} else {
				this.setRatio(BigDecimal.ZERO);
				this.setReceived(0);
			}
		} else {
			boolean check = false;
			switch (this.betType) {
				case BIG:
					check = diceLog.hasBig();
					break;
				case SMALL:
					check = diceLog.hasSmall();
					break;
				case SINGLE:
					check = diceLog.hasSingle();
					break;
				case DOUBLE:
					check = diceLog.hasDouble();
					break;
			}
			if (check) {
				this.setRatio(betType.getRatio());
				this.setReceived(betType.getRatio().intValue() * golds);
			} else {
				this.setRatio(BigDecimal.ZERO);
				this.setReceived(0);
			}
		}
	}

}
